package com.ruoyi.web.controller.app.api.controller;

import com.alibaba.fastjson.JSON;
import com.ruoyi.applet.appletShoppingCart.domain.AppletShoppingCart;
import com.ruoyi.applet.appletShoppingCart.service.IAppletShoppingCartService;
import com.ruoyi.applet.entity.PageR;
import com.ruoyi.applet.entity.ProductApplet;
import com.ruoyi.applet.entity.ShoppingCartApplet;
import com.ruoyi.applet.product.domain.Product;
import com.ruoyi.applet.product.domain.ProductCategories;
import com.ruoyi.applet.product.domain.ProductSpecifications;
import com.ruoyi.applet.product.service.IProductCategoriesService;
import com.ruoyi.applet.product.service.IProductService;
import com.ruoyi.applet.product.service.IProductSpecificationsService;
import com.ruoyi.common.core.domain.AjaxResult;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * @描述: 产品相关接口
 * @Author: TangYuan
 * @Date: 2024/11/20 15:22
 */
@RestController
@RequestMapping("/wxApi/product")
public class ProductAppletController {
    
    @Resource
    private IProductService productService;
    @Resource
    private IProductCategoriesService productCategoriesService;
    @Resource
    private IProductSpecificationsService productSpecificationsService;
    @Resource
    private IAppletShoppingCartService appletShoppingCartService;
    
    /**
     * 获取所有分类
     */
    @GetMapping("/getCategories")
    public AjaxResult getBanner() {
        List<ProductCategories> productCategories = productCategoriesService.selectProductCategoriesListApplet(null);
        return AjaxResult.success(productCategories);
    }
    
    /**
     * 首页获取产品数据
     */
    @PostMapping("/getProduct")
    public AjaxResult getProduct(@RequestBody ProductApplet product) {
        product.setStatus(1);
        PageR<Product> productList = productService.selectProductListApplet(product);
        for (Product product1 : productList.getResult()) {
            List<String> list = JSON.parseArray(product1.getPicturesPath(), String.class);
            if (list != null && !list.isEmpty()) {
                product1.setPicturesPath(list.get(0));
            }
        }
        return AjaxResult.success(productList);
    }
    
    /**
     * 获取热门产品数据
     */
    @PostMapping("/getProductByHot")
    public AjaxResult getProductByHot(@RequestBody ProductApplet product) {
        product.setStatus(1);
        PageR<Product> productList = productService.selectProductListByHotApplet(product);
        for (Product product1 : productList.getResult()) {
            List<String> list = JSON.parseArray(product1.getPicturesPath(), String.class);
            if (list != null && !list.isEmpty()) {
                product1.setPicturesPath(list.get(0));
            }
        }
        return AjaxResult.success(productList);
    }
    
    /**
     * 获取最新产品
     */
    @PostMapping("/getProductByNew")
    public AjaxResult getProductByNew(@RequestBody ProductApplet product) {
        product.setStatus(1);
        PageR<Product> productList = productService.selectProductListByNewApplet(product);
        for (Product product1 : productList.getResult()) {
            List<String> list = JSON.parseArray(product1.getPicturesPath(), String.class);
            if (list != null && !list.isEmpty()) {
                product1.setPicturesPath(list.get(0));
            }
        }
        return AjaxResult.success(productList);
    }
    
    /**
     * 获取产品详细信息
     */
    @PostMapping("/getProductInfo")
    public AjaxResult getProductInfo(@RequestBody ProductApplet productApplet) {
        Product product = productService.selectProductById(productApplet.getId());
        ProductCategories productCategories = productCategoriesService.selectProductCategoriesById(product.getCategoriesId());
        product.setCategories(productCategories.getName());
        product.setPicturesPathList(JSON.parseArray(product.getPicturesPath(), String.class));
        List<ProductSpecifications> productSpecificationsList = productSpecificationsService.selectProductSpecificationsListByProductId(product.getId());
        product.setSpecificationsList(productSpecificationsList);
        product.setSpecificationName(productSpecificationsList.get(0).getSpecificationsName());
        product.setPrice(productSpecificationsList.get(0).getPrice());
        product.setUnit(productSpecificationsList.get(0).getUnit());
        product.setSpecificationId(productSpecificationsList.get(0).getId());
        return AjaxResult.success(product);
    }
    
    /**
     * 加入购物车
     */
    @PostMapping("/addShoppingCart")
    public AjaxResult addShoppingCart(@RequestBody AppletShoppingCart shoppingCart) {
        if (shoppingCart.getWxUserId() == null || shoppingCart.getProductId() == null || shoppingCart.getSpecificationId() == null) {
            return AjaxResult.error("请求错误");
        }
        if (shoppingCart.getQuantity() == null) {
            shoppingCart.setQuantity(1L);
        }
        AppletShoppingCart appletShoppingCart = appletShoppingCartService.selectAppletShoppingCartListBySole(shoppingCart);
        int i = 0;
        if (appletShoppingCart == null) {
            i = appletShoppingCartService.insertAppletShoppingCartApplet(shoppingCart);
        } else {
            appletShoppingCart.setQuantity(appletShoppingCart.getQuantity() + shoppingCart.getQuantity());
            i = appletShoppingCartService.updateAppletShoppingCart(appletShoppingCart);
        }
        return AjaxResult.success(i);
    }
    
    /**
     * 获取购物车数据
     */
    @PostMapping("/getShoppingCart")
    public AjaxResult getShoppingCart(@RequestBody ShoppingCartApplet shoppingCartApplet) {
        if (shoppingCartApplet.getWxUserId() == null) {
            return AjaxResult.error("请先登录");
        }
        PageR<AppletShoppingCart> shoppingCartList = appletShoppingCartService.selectShoppingCartListApplet(shoppingCartApplet);
        for (AppletShoppingCart appletShoppingCart : shoppingCartList.getResult()) {
            List<String> list = JSON.parseArray(appletShoppingCart.getPicturesPath(), String.class);
            if (list != null && !list.isEmpty()) {
                appletShoppingCart.setPicturesPath(list.get(0));
            }
        }
        return AjaxResult.success(shoppingCartList);
    }
    
    /**
     * 删除购物车
     */
    @PostMapping("/delShoppingCart")
    public AjaxResult delShoppingCart(@RequestBody AppletShoppingCart shoppingCart) {
        if (shoppingCart.getId() == null) {
            return AjaxResult.error("请求错误");
        }
        int i = appletShoppingCartService.deleteAppletShoppingCartById(shoppingCart.getId());
        return AjaxResult.success(i);
    }
    
    /**
     * 修改购物车
     */
    @PostMapping("/updateShoppingCart")
    public AjaxResult updateShoppingCart(@RequestBody AppletShoppingCart shoppingCart) {
        if (shoppingCart.getId() == null) {
            return AjaxResult.error("请求错误");
        }
        int i = appletShoppingCartService.updateAppletShoppingCart(shoppingCart);
        return AjaxResult.success(i);
    }
}
